$('.attitude').click(function(event) {
    event.preventDefault();
    var likeBtn = $(this); // 保存当前点击的点赞按钮

    var article_id = likeBtn.data("article-id");
    var attitudeNum1 = $("#attitude_num_1[data-article-id='" + article_id + "']");
    var attitudeNum2 = $("#attitude_num_2[data-article-id='" + article_id + "']");
//    alert(article_id);
    $.ajax({
        url: '/up/' + article_id,
        method: 'post',
        data: {'num':attitudeNum1.val()},
        success: function(res) {
            if (res.code==1){
                    attitudeNum1.text(res.data);
//                    $('.attitude').removeClass('up_like')
                    likeBtn.removeClass('up_like')
//                    $('.attitude').addClass('up_like_over')
                    likeBtn.addClass('up_like_over')
            }
            else if(res.code == 3){
                $('#attitude_num_2').text(res.data);
                $('.bad').removeClass('down_dislike_over')
                $('.bad').addClass('down_dislike')

                $('#attitude_num_1').text(res.data2);
                likeBtn.removeClass('up_like')
                likeBtn.addClass('up_like_over')
            }
            else{
                attitudeNum1.text(res.data);
                likeBtn.removeClass('up_like_over')
                likeBtn.addClass('up_like')
            }
        }
    })
})

$('.bad').click(function(event) {
    event.preventDefault();
    var article_id = $('.bad').data("article-id");
    var likeBtn = $(this); // 保存当前点击的点赞按钮

    $.ajax({
        url: '/down/' + article_id,
        method: 'post',
        data: {'num':$('#attitude_num_2').val()},
        success: function(res) {
            if (res.code==1){
                    $('#attitude_num_2').text(res.data);
                    likeBtn.removeClass('down_dislike')
                    likeBtn.addClass('down_dislike_over')
            }
            else if(res.code == 3){
                $('#attitude_num_1').text(res.data);
                likeBtn.removeClass('down_dislike')
                likeBtn.addClass('down_dislike_over')

                $('#attitude_num_2').text(res.data2);
                likeBtn.removeClass('up_like_over')
                likeBtn.addClass('up_like')
            }
            else{
                $('#attitude_num_2').text(res.data);
                likeBtn.removeClass('down_dislike_over')
                likeBtn.addClass('down_dislike')
            }


        }
    })

});

$(document).ready(function(){
    $(document).on('click','#support',function() {
        var article_id = $(this).data('article-id');
        var change = $(this);
        var oppose = $('#oppose');
        $.ajax({
            url: '/like?article_id=' + article_id,
            method: 'POST',
            success: function(data){
                if(data.code == 0){
                    alert('请先登录!');
                }
                else if(data.code == 1){
                    var newCount = document.createTextNode(data.num);
                    change.empty();
                    change.append($('<img>', {src: '../static/icon/点赞完.svg', alt: '', width: '24px', height: '24px'})); // 添加图片
                    change.append('\u00A0\u00A0\u00A0'); // 添加一些空格
                    change.append(newCount); // 添加新的数量


                }
                else if(data.code == 2){
                    var newCount = document.createTextNode(data.num);
                    change.empty();
                    change.append($('<img>', {src: '../static/icon/点赞.svg', alt: '', width: '24px', height: '24px'})); // 添加图片
                    change.append('\u00A0\u00A0\u00A0'); // 添加一些空格
                    change.append(newCount); // 添加新的数量\
                }
            }
        })
    });
    $(document).on('click','#oppose',function() {
        var article_id = $(this).data('article-id');
        var change = $(this);
        $.ajax({
            url: '/dlike?article_id=' + article_id,
            method: 'POST',
            success: function(data){
                if(data.code == 0){
                    alert('请先登录!');
                }
                else if(data.code == 1){
                    var newCount = document.createTextNode(data.num);
                    change.empty();
                    change.append($('<img>', {src: '../static/icon/点踩完.svg', alt: '', width: '24px', height: '24px'})); // 添加图片
                    change.append('\u00A0\u00A0\u00A0'); // 添加一些空格
                    change.append(newCount); // 添加新的数量


                }
                else if(data.code == 2){
                    var newCount = document.createTextNode(data.num);
                    change.empty();
                    change.append($('<img>', {src: '../static/icon/点踩.svg', alt: '', width: '24px', height: '24px'})); // 添加图片
                    change.append('\u00A0\u00A0\u00A0'); // 添加一些空格
                    change.append(newCount); // 添加新的数量\
                }
            }
        })
    })
})





